package com.github.binarywang.demo.wx.miniapp.error;


import com.github.binarywang.demo.wx.miniapp.domain.ResponseEntity;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(WxErrorException.class)
    @ResponseBody
    public ResponseEntity handleAllExceptions(Exception ex) {
        // 记录日志，处理其他逻辑
        ResponseEntity entity = new ResponseEntity();
        entity.setCode("500");
        entity.setMessage("An error occurred: " + ex.getMessage());
        return entity;
        //return new ResponseEntity<>("An error occurred: " + ex.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
    }

//    @Override
//    protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
//                                                                  HttpHeaders headers,
//                                                                  HttpStatus status,
//                                                                  WebRequest request) {
//
//        ResponseEntity entity = new ResponseEntity();
//        entity.setCode("500");
//        entity.setMessage("Validation failed: " + ex.getBindingResult().toString());
//        return entity;
//        // 记录日志，处理其他逻辑
//       //return new ResponseEntity<>("Validation failed: " + ex.getBindingResult().toString(), HttpStatus.BAD_REQUEST);
//    }
}
